<?php

use Dcat\Admin\Grid;
use Dcat\Admin\Form;
use Dcat\Admin\Show;

Grid::resolving(function (Grid $grid) {
    $grid->withBorder();
    $grid->disableRowSelector();
    $grid->toolsWithOutline();
    $grid->disableViewButton();
    $grid->scrollbarX();
    $grid->model()->orderBy('id','desc');
    $grid->addTableClass(['table-text-center']);
});

Show::resolving(function (Show $show){
    $show->disableDeleteButton();
    $show->disableEditButton();
});

Form::resolving(function (Form $form){
    $form->disableDeleteButton();
    $form->disableEditingCheck();
    $form->disableViewCheck();
    $form->disableViewButton();
});


Form\Field\Editor::resolving(function (Form\Field\Editor $editor) {
    //$editor->imageDirectory(EditorImageUploadPath)->height(400); // 如果不修改配置可以删掉 EditorImageUploadPath是我自定义的图片保存路径
    $ref = new \ReflectionClass(get_class($editor));
    $method = $ref->getMethod('defaultImageUploadUrl');
    $method->setAccessible(true);
    $editor->disk('oss');
    $url = $method->invoke($editor);
    $editor->options([
        'plugins'               => [
            'advlist',
            'autolink',
            'link',
            'image',
            'axupimgs',
            'media',
            'lists',
            'preview',
            'code',
            'help',
            'fullscreen',
            'table',
            'autoresize',
            'codesample',
            '',
        ],
        'toolbar'               => [
            'undo redo | preview fullscreen | styleselect | fontsizeselect bold italic underline strikethrough forecolor backcolor | link image axupimgs media blockquote removeformat codesample',
            'alignleft aligncenter alignright  alignjustify| indent outdent bullist numlist table subscript superscript | code',
        ],// plugins和toolbar添加axupimgs
        'images_upload_handler' =>\Dcat\Admin\Support\JavaScript::make(
            <<<JS
                                  function (blobInfo, succFun, failFun) {
       var xhr, formData;
       var file = blobInfo.blob();
       xhr = new XMLHttpRequest();
       xhr.withCredentials = false;
       xhr.open('POST', "$url"); // 图片上传url
       xhr.onload = function() {
           var json;
           if (xhr.status != 200) {
               failFun('HTTP Error: ' + xhr.status);
               return;
           }
           json = JSON.parse(xhr.responseText);
           if (!json || typeof json.location != 'string') {
               failFun('Invalid JSON: ' + xhr.responseText);
               return;
           }
           succFun(json.location);
       };
       formData = new FormData();
       formData.append('file', file, file.name );
       xhr.send(formData);
   }
JS
        ), // 直接写会被当成字符串，而不是js函数
    ]);
});

